$(document).ready(function() {
	var materialVue=new Vue({
		el : '#showMaterial',
		data : {
			items : null
		},
		methods : {
			onclick : function(material) {
				openChooseQuantityBox(material);
			}
		}
	});
	
	getAllMaterial();

	var materialList = new Array();
	var materialObject;

	function getAllMaterial() {
		baseUtil.httpSend("material/getAllMaterialAjax", null, setMaterialVue);
	}	
	
	// 搜索按钮的点击事件
	$("#searchMaterialButton").on("click", function() {
		var data={"searchWords":$("#inputSearchWords").val()};
		baseUtil.httpSend("material/getMaterialByMaterialIdOrMaterialNameAJAX", data, setMaterialVue);
	});
	
	
	function setMaterialVue(result) {
		Vue.set(materialVue,"items",result);
	}
	
});




function Material(id,materialId, materialName, materialQuantity, unitId,unitName,materialPrice,totalPrice) {
	this.id = id;
	this.materialId = materialId;
	this.materialName = materialName;
	this.materialQuantity = materialQuantity;
	this.unitId = unitId;
	this.unitName = unitName;
	this.materialPrice = materialPrice;
	this.totalPrice = totalPrice;
}

function openChooseQuantityBox(material) {
	chooseMaterialQuantityBox = layer.open({
		type : 2,
		offset : '10%',
		maxmin : true,
		title :'设置数量',
		area : [ '85%', '85%' ],
		btn : [ '确认', '取消' ] // 只是为了演示
		,
		yes : function() {
			// 获得子窗口的值
			var childBox = layer.getChildFrame('body',
					chooseMaterialQuantityBox);
			var materialQuantity = childBox.find("#inputMaterialQuantity")
					.val();
			var a = $("#materialList").val();
			if (a != "") {
				a = eval('(' + a + ')');//要加这句才能获得json数组里面的数据
			}

			var tempList = new Array();

			if (a != "") {
				for ( var i in a) {
					tempList.push(new Material(null,a[i].materialId,
							a[i].materialName, a[i].materialQuantity,
							a[i].unitId,a[i].unitName,a[i].materialPrice,a[i].totalPrice));
				}
			}

			tempList.push(new Material(null,material.materialId,
					material.materialName, materialQuantity, material.unitId,material.unit.unitName,material.materialPrice,materialQuantity*material.materialPrice));


			$("#materialList").val(JSON.stringify(tempList));

		layer.close(chooseMaterialQuantityBox);

		},
		btn2 : function() {
			layer.close();
		},
		shadeClose : true, // 点击遮罩关闭
		content : SERVER_PRE_URL
				+ 'material/getMaterialByPrimaryKeyReturnTo?materialId='
				+ material.materialId + '&returnTo=chooseMaterialQuantity'
	});

}
